package com.easyOrder.core.utils;

import com.google.common.collect.Lists;

import java.util.List;

/**
 * Created by yanbin on 2017/4/18.
 */
public class ListHelper {

    /**
     * 分页
     * @param result
     * @param pageIndex
     * @param pageSize
     * @param <T>
     * @return
     */
    public static <T> List<T> page(List<T> result, Integer pageIndex, Integer pageSize) {
        if (pageIndex <= 0 || pageSize <= 0 || result.size() == 0) {
            return Lists.newArrayList();
        }
        Integer totalCount = result.size();
        if (pageIndex <= 0) {
            pageIndex = 1;
        }
        Integer indexStart = pageSize * (pageIndex - 1);
        Integer indexEnd = indexStart + pageSize;
        if (indexEnd > totalCount) {
            indexEnd = totalCount;
        }
        return result.subList(indexStart, indexEnd);
    }
}
